﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SocketHttpLib
{
    public class SystemSR
    {
        static Assembly asm;
        public static string GetString(string name, params object[] args)
        {
            return systemSRGetString.Invoke(null, new object[] { name, args }) as string;
        }
        private static MethodInfo systemSRGetString;
        static SystemSR()
        {
            asm = Assembly.Load("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            var srType = asm.GetType("System.SR");
            var methods = srType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
            systemSRGetString = methods[0];
        }
        public static object CreateInstance(string typeName, params object[] args)
        {
            var srType = asm.GetType(typeName);
            return Activator.CreateInstance(srType, args);
        }

        public static object GetStaticNonPublicField(string typeName, string fieldName)
        {
            var srType = asm.GetType(typeName);
            var field = srType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static);
            return field.GetValue(null);
        }

        public static object InvokeStaticNonPublicMethod(string typeName, string methodName, object[] parameters)
        {
            var srType = asm.GetType(typeName);
            var method = srType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static);
            return method.Invoke(null, parameters);
        }

        public static object InvokeInstanceNonPublicMethod(object instance, string methodName, object[] parameters)
        {
            var method = instance.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
            return method.Invoke(instance, parameters);
        }
    }
}
